package com.boc.langxiang.ui.adapter.me.section;

import android.content.Context;
import android.view.View;
import android.widget.ImageView;

import com.boc.langxiang.R;
import com.boc.langxiang.contract.me.OrdersReturnProductContract;
import com.boc.langxiang.entity.response.ProductEntity;
import com.boc.langxiang.entity.response.ProductListEntity;
import com.boc.langxiang.manager.PicassoImageLoader;
import com.boc.langxiang.util.StringUtil;
import com.chad.library.adapter.base.BaseSectionFootQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;

import java.util.List;

public class OrdersRPSectionAdapter extends BaseSectionFootQuickAdapter<OrdersSection> {

    private OrdersReturnProductContract.View mView;

    /**
     * Same as QuickAdapter#QuickAdapter(Context,int) but with
     * some initialization data.
     *
     * @param context          The context.
     * @param layoutResId      The layout resource id of each item.
     * @param sectionHeadResId The section head layout id for each item
     * @param footResId
     * @param data             A new list is created out of this one to avoid mutable list
     */
    public OrdersRPSectionAdapter(Context context, int layoutResId, int sectionHeadResId, int footResId, List<OrdersSection> data, OrdersReturnProductContract.View view) {
        super(layoutResId, sectionHeadResId, footResId, data);
        this.mView = view;

    }

    @Override
    protected void convertHead(final BaseViewHolder helper, final OrdersSection item) {
        final ProductListEntity entity = item.getProductListEntity();
        helper.setText(R.id.tv_me_orders_numbers, StringUtil.isEmpty(entity.getOrderid()) ? "" : entity.getOrderid())
                .setText(R.id.tv_me_orders_status, getStatus(entity.getStatus()))
                .setOnClickListener(R.id.linlayout_me_orders_head, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mView.startDetailActivity(entity.getId(), entity.getPrice(), entity.getStatus());
                    }
                });
    }

    @Override
    protected void convertFoot(final BaseViewHolder helper, final OrdersSection item) {
        final ProductListEntity entity = item.getProductListEntity();
        helper.setText(R.id.tv_me_orders_count, "共" + entity.getProductnumber() + "件")
                .setText(R.id.tv_me_orders_totalprice, StringUtil.priceExecute(StringUtil.setPrice(entity.getPrice())))
                .setOnClickListener(R.id.linlayout_me_orders_bottom, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mView.startDetailActivity(entity.getId(), entity.getPrice(), entity.getStatus());
                    }
                });
    }

    @Override
    protected void convert(final BaseViewHolder helper, OrdersSection item) {
        final ProductEntity entity = item.t;
        helper.setText(R.id.tv_me_orders_content, StringUtil.isEmpty(entity.getTitle()) ? "" : entity.getTitle())
//                .setText(R.id.tv_me_orders_realsize, StringUtil.isEmpty(entity.getSize()) ? "" : entity.getSize())
                .setText(R.id.tv_me_orders_price, "¥" + (StringUtil.isEmpty(entity.getPrice()) ? "" : entity.getPrice()))
                .setText(R.id.tv_me_orders_num, "×" + (StringUtil.isEmpty(entity.getNumber()) ? "" : entity.getNumber()))
                .setOnClickListener(R.id.linlayout_me_orders_entity, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mView.startDetailActivity(entity.getId(), entity.getTotalPrice(), entity.getStatus());
                    }
                });

        PicassoImageLoader.getInstance().displayUrlImage(mContext, entity.getPhotourl()
                , (ImageView) helper.getView(R.id.esv_me_orders_img));
    }

    private String getStatus(String status) {
        if ("5".equals(status)) {
            return "申请退款";
        } else if ("6".equals(status)) {
            return "退款退货完成";
        } else if ("7".equals(status)) {
            return "退款退货失败";
        } else {
            return "无效订单";
        }
    }
}
